package com.fcy.teamwork.business.projectlife

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import com.fcy.teamwork.R
import com.fcy.teamwork.base.log
import com.fcy.teamwork.databinding.BottomSheetFragmentAddNewSkillBinding
import com.fcy.teamwork.pojo.PersonProject

class DeleteFromGroupDialog : DialogFragment() {
    private val vm by activityViewModels<ProjectControllerVM>()
    var listsner: (Boolean) -> Unit = {}
    private lateinit var binding: BottomSheetFragmentAddNewSkillBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        super.onCreateView(inflater, container, savedInstanceState)
        binding = BottomSheetFragmentAddNewSkillBinding.inflate(inflater)
        return binding.root
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dia = super.onCreateDialog(savedInstanceState)
        dia.window?.setLayout(500, 400)
        dia.window?.decorView?.setBackgroundResource(R.drawable.bottom_sheet_shape)
        return dia
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        log(requireActivity())
        binding.actionBar.apply {
            tvTitle.text = "请给予该生这段时间表现评价"
            ibFinish.visibility = View.VISIBLE
            ibFinish.setOnClickListener {
                if (binding.etNewSkill.text.toString().isBlank()) {
                    Toast.makeText(requireContext(), "请给学生一些评价", Toast.LENGTH_SHORT).show()
                } else lifecycleScope.launchWhenCreated {
                    vm.removeFromGroup(PersonProject().apply {
                        arguments?.apply {
                            personId = getString("personId")
                            projectId = getString("projectId")
                            ext = binding.etNewSkill.text.toString()
                        }
                    })
                    listsner(true)
                    dismiss()
                }
            }
            ibBack.setOnClickListener {
                listsner(false)
                dismiss()
            }
        }
    }
}